package com.xinjian.jianba.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xinjian.jianba.dto.UserRelationDto;
import com.xinjian.jianba.exception.ResponseCode;
import com.xinjian.jianba.service.UserRelationService;
import com.xinjian.jianba.service.ValidatorService;
import com.xinjian.jianba.utils.JsonUtil;

@RestController
@RequestMapping(value = "/api/v3/jianba/relations")
public class UserRelationControllor extends AbstractController {
	@Autowired
	private UserRelationService userRelationService;
	@Autowired
	private ValidatorService validatorService;

	@PostMapping("/black")
	public String modify(@RequestBody UserRelationDto userRelationDto) {

		try {
			validatorService.valid(userRelationDto);
		} catch (Exception e) {
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, e.getMessage());
		}
		userRelationService.black(userRelationDto);
		return this.responseClientJsonData(ResponseCode.SUCCESS, "拉黑成功");

	}

	@PostMapping("/cancleBlack")
	public String cancleBlack(@RequestBody UserRelationDto userRelationDto) {
		try {
			validatorService.valid(userRelationDto);
		} catch (Exception e) {
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, e.getMessage());
		}
		userRelationService.cancleBlack(userRelationDto);
		return this.responseClientJsonData(ResponseCode.SUCCESS, "取消拉黑成功");
	}

	@PostMapping("/focus")
	public String focus(@RequestBody UserRelationDto userRelationDto) {
		try {
			validatorService.valid(userRelationDto);
		} catch (Exception e) {
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, e.getMessage());
		}
		int re = userRelationService.focus(userRelationDto);
		if (re == 3) {
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, "无法关注已被拉黑");
		} else if (re == 2) {
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, "不能重复关注");
		} else if(re==4){
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, "您已拉黑此人");
		}else {
			return this.responseClientJsonData(ResponseCode.SUCCESS, "关注成功");
		}
	}

	@PostMapping("/cancleFocus")
	public String cancleFocus(@RequestBody UserRelationDto userRelationDto) {
		try {
			validatorService.valid(userRelationDto);
		} catch (Exception e) {
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, e.getMessage());
		}
		userRelationService.cancleFocus(userRelationDto);
		return this.responseClientJsonData(ResponseCode.SUCCESS, "取消关注成功");
	}

	@PostMapping("/acquireFocus")
	public String acquireFocus(@RequestBody Map<String,String> map) {
		String userId=map.get("userId");
		if (userId == null || userId.equals("")) {
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, "用户Id不能为空");
		}
		Map<String, Object> mapRe = userRelationService.getFocusAndCountsByUserId(Long.parseLong(userId));
		mapRe.put("code", 200);
		mapRe.put("message", ResponseCode.SUCCESS_MSG);
		String re = JsonUtil.getJsonString(mapRe);
		return re;
	}

	@PostMapping("/acquireBlack")
	public String acquireBlack(@RequestBody Map<String,String> map) {
		String userId=map.get("userId");
		if (userId == null || userId.equals("")) {
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, "用户Id不能为空");
		}
		Map<String, Object> mapRe = userRelationService.getBlacksAndCountsByUserId(Long.parseLong(userId));
		mapRe.put("code", 200);
		mapRe.put("message", ResponseCode.SUCCESS_MSG);
		String re = JsonUtil.getJsonString(mapRe);
		return re;
	}

}
